<?php
class ModelCatalogAttribute extends Model {
	public function addAttribute($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "attribute SET attribute_group_id = '" . (int) $data['attribute_group_id'] . "', sort_order = '" . (int) $data['sort_order'] . "'");
		
		$attribute_id = $this->db->getLastId();
		
		foreach ($data['attribute_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "attribute_description SET attribute_id = '" . (int) $attribute_id . "', language_id = '" . (int) $language_id . "', name = '" . $this->db->escape($value['name']) . "'");
		}
	}
	public function editAttribute($attribute_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "attribute SET attribute_group_id = '" . (int) $data['attribute_group_id'] . "', sort_order = '" . (int) $data['sort_order'] . "' WHERE attribute_id = '" . (int) $attribute_id . "' AND product_cat=0");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int) $attribute_id . "'");
		
		foreach ($data['attribute_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "attribute_description SET attribute_id = '" . (int) $attribute_id . "', language_id = '" . (int) $language_id . "', name = '" . $this->db->escape($value['name']) . "'");
		}
	}
	public function deleteAttribute($attribute_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute WHERE attribute_id = '" . (int) $attribute_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int) $attribute_id . "'");
	}
	public function getAttribute($attribute_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute WHERE attribute_id = '" . (int) $attribute_id . "' AND product_cat=0 LIMIT 1");
		
		return $query->row;
	}
	public function getAttributes($data = array()) {
		$sql = "SELECT *, (SELECT agd.name FROM " . DB_PREFIX . "attribute_group_description agd WHERE agd.attribute_group_id = a.attribute_group_id AND agd.language_id = '" . (int) $this->config->get('config_language_id') . "') AS attribute_group FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE ad.language_id = '" . (int) $this->config->get('config_language_id') . "'";
		
		if (!empty($data['filter_name'])) {
			$sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}
		
		if (!empty($data['filter_attribute_group_id'])) {
			$sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
		}
		
		$sort_data = array( 
			'ad.name', 
			'attribute_group', 
			'a.sort_order' 
		);
		
		$sql .= "group by a.attribute_id";
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		}
		else {
			$sql .= " ORDER BY attribute_group, ad.name";
		}
		
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		}
		else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}
			
			$sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
		}
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	public function getAttributeDescriptions($attribute_id) {
		$attribute_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int) $attribute_id . "'");
		
		foreach ($query->rows as $result) {
			$attribute_data[$result['language_id']] = array( 
				'name' => $result['name'] 
			);
		}
		
		return $attribute_data;
	}
	public function getAttributesByAttributeGroupId($data = array()) {
		$sql = "SELECT *, (SELECT agd.name FROM " . DB_PREFIX . "attribute_group_description agd WHERE agd.attribute_group_id = a.attribute_group_id AND agd.language_id = '" . (int) $this->config->get('config_language_id') . "') AS attribute_group FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE ad.language_id = '" . (int) $this->config->get('config_language_id') . "'";
		
		if (!empty($data['filter_name'])) {
			$sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}
		
		if (!empty($data['filter_attribute_group_id'])) {
			$sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
		}
		
		$sort_data = array( 
			'ad.name', 
			'attribute_group', 
			'a.sort_order' 
		);
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		}
		else {
			$sql .= " ORDER BY ad.name";
		}
		
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		}
		else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}
			
			$sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
		}
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	public function getTotalAttributes() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "attribute");
		
		return $query->row['total'];
	}
	public function getTotalAttributesByAttributeGroupId($attribute_group_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "attribute WHERE attribute_group_id = '" . (int) $attribute_group_id . "'");
		
		return $query->row['total'];
	}
	
	/**
	 * 查询属性已绑定的产品类别
	 *
	 * @param unknown_type $attribute_id        	
	 */
	public function getSelectCat($attribute_id) {
		$query = $this->db->query("SELECT a.*,ad.name FROM " . DB_PREFIX . "attribute a left join " . DB_PREFIX . "attribute_description ad WHERE a.attribute_id=ad.attribute_id AND a.attribute_id={$attribute_id}");
		return $query->rows;
	}
	/**
	 * 查询产品类别
	 */
	public function getSelectCatByPid($pid = 0) {
		$query = $this->db->query("SELECT a.*,ad.name FROM " . DB_PREFIX . "attribute a left join " . DB_PREFIX . "attribute_description ad ON a.attribute_id=ad.attribute_id WHERE a.product_cat={$pid} AND ad.language_id = " . (int) $this->config->get('config_language_id'));
		return $query->rows;
	}
	/**
	 * 增加属性值
	 */
	public function add_attr($data) {
		// 		$is_required = isset($data['is_required']) ? (int) ($data['is_required']) : 0;
		// 		$is_default = isset($data['is_default']) ? (int) ($data['is_default']) : 0;
		// 		$is_screen = isset($data['is_screen']) ? (int) ($data['is_screen']) : 0;
		// 		$order = isset($data['order']) ? (int) ($data['order']) : 0;
		// 		// 		return json_encode($data);
		// 		return $this->db->query(" INSERT INTO " . DB_PREFIX . "attribute(attribute_id,product_cat,is_required,is_default,is_screen,sort_order)
		// 									VALUES({$data['id']}," . intval($data['product_cat']) . ",{$is_required},{$is_default},{$is_screen},{$order})");
		$this->db->query(" INSERT INTO " . DB_PREFIX . "attribute(attribute_group_id,is_default,is_screen,sort_order)
								  VALUES({$data['attribute_group_id']}," . (int) $data['is_default'] . "," . (int) $data['is_screen'] . "," . (int) $data['order'] . ")");
		$attribute_id = $this->db->getLastId();
		if ($attribute_id != 0) {
			return $this->db->query(" INSERT INTO " . DB_PREFIX . "attribute_description(attribute_id,language_id,name)
								  VALUES({$attribute_id}," . (int) $this->config->get('config_language_id') . ",'{$data['name']}')");
		}
		else {
			return 0;
		}
	}
	public function add_attr_group($data) {
		$this->db->query(" INSERT INTO " . DB_PREFIX . "attribute_group(category_id,sort_order)
				VALUES(" . (int) $data['category_id'] . "," . (int) $data['order'] . ")");
		$attribute_group_id = $this->db->getLastId();
		if ($attribute_group_id != 0) {
			return $this->db->query(" INSERT INTO " . DB_PREFIX . "attribute_group_description(attribute_group_id,language_id,name)
						VALUES({$attribute_group_id}," . (int) $this->config->get('config_language_id') . ",'{$data['name']}')");
		}
		else {
			return 0;
		}
	}
	
	/**
	 * 删除属性值
	 */
	public function del_attr($data) {
		return $this->db->query(" DELETE FROM " . DB_PREFIX . "attribute 
								  WHERE attribute_id={$data['attribute_id']}");
	}
	public function del_attr_group($data) {
		return $this->db->query(" DELETE FROM " . DB_PREFIX . "attribute_group
					WHERE attribute_group_id={$data['attribute_group_id']}");
	}
	public function getCategoryAttributes($category_id) {
		$query = $this->db->query(" SELECT a.attribute_id,a.sort_order,a.is_default,a.is_screen,ap.sort_order `order`,ad.name,agd.name group_name,ap.attribute_group_id  
									FROM " . DB_PREFIX . "attribute_group ap 
									LEFT JOIN " . DB_PREFIX . "attribute a ON a.attribute_group_id=ap.attribute_group_id 
									LEFT JOIN " . DB_PREFIX . "attribute_group_description agd ON ap.attribute_group_id=agd.attribute_group_id AND agd.language_id=" . (int) $this->config->get('config_language_id') . "
									LEFT JOIN " . DB_PREFIX . "attribute_description ad ON a.attribute_id=ad.attribute_id AND ad.language_id=" . (int) $this->config->get('config_language_id') . "  
									WHERE ap.category_id={$category_id}  
									ORDER BY ap.sort_order,agd.name,a.sort_order,ad.name");
		return $query->rows;
	}
}
?>